/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.meibang.consultation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.meibang.consultation.entity.BeautyConsultationOrder;
import cn.apestar.modules.meibang.consultation.enums.ConsultationEnum;
import cn.apestar.modules.meibang.consultation.service.BeautyConsultationOrderService;

/**
 * consultationController
 * @author consultation
 * @version 2017-06-03
 */
@Controller
@RequestMapping(value = "${adminPath}/consultation")
public class BeautyConsultationOrderController extends BaseController {

    @Autowired
    private BeautyConsultationOrderService beautyConsultationOrderService;

    @ModelAttribute
    public BeautyConsultationOrder get(@RequestParam(required = false) String id) {
        BeautyConsultationOrder entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = beautyConsultationOrderService.get(id);
        }
        if (entity == null) {
            entity = new BeautyConsultationOrder();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(BeautyConsultationOrder beautyConsultationOrder, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<BeautyConsultationOrder> page = beautyConsultationOrderService.findPage(new Page<BeautyConsultationOrder>(request, response),
                                                                                     beautyConsultationOrder);
        model.addAttribute("page", page);
        return "modules/meibang/consultation/beautyConsultationOrderList";

    }

    @RequestMapping(value = "form")
    public String form(BeautyConsultationOrder beautyConsultationOrder, Model model) {
        model.addAttribute("beautyConsultationOrder", beautyConsultationOrder);
        return "modules/meibang/consultation/beautyConsultationOrderForm";
    }

    @RequestMapping(value = "questionForm")
    public String questionForm(BeautyConsultationOrder beautyConsultationOrder, Model model) {
        model.addAttribute("beautyConsultationOrder", beautyConsultationOrder);
        return "modules/meibang/consultation/beautyConsultationOrderQuestionForm";
    }

    @RequestMapping(value = "save")
    public String save(BeautyConsultationOrder beautyConsultationOrder, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, beautyConsultationOrder)) {
            return form(beautyConsultationOrder, model);
        }
        beautyConsultationOrderService.save(beautyConsultationOrder);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(BeautyConsultationOrder beautyConsultationOrder, RedirectAttributes redirectAttributes) {
        beautyConsultationOrderService.delete(beautyConsultationOrder);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/?repage";
    }

    @RequestMapping(value = "updateStatus")
    public String updateStatus(BeautyConsultationOrder beautyConsultationOrder, RedirectAttributes redirectAttributes) {

        if (beautyConsultationOrder.getStatus() != null) {

            if (ConsultationEnum.USE.getValue().equals(beautyConsultationOrder.getStatus())) {

                beautyConsultationOrder.setStatus(ConsultationEnum.STOP.getValue());

            } else if (ConsultationEnum.STOP.getValue().equals(beautyConsultationOrder.getStatus())) {

                beautyConsultationOrder.setStatus(ConsultationEnum.USE.getValue());

            }

        }

        System.out.println(beautyConsultationOrder.getStatus() + "," + beautyConsultationOrder.getId());

        beautyConsultationOrderService.save(beautyConsultationOrder);
        addMessage(redirectAttributes, "更新成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/?repage";
    }
}